package com.nuoyun.tnvmue.activity

import android.os.Bundle
import android.view.MenuItem
import androidx.fragment.app.Fragment
import androidx.viewpager.widget.ViewPager.OnPageChangeListener
import com.google.android.material.bottomnavigation.BottomNavigationView
import com.nuoyun.tnvmue.R
import com.nuoyun.tnvmue.activity.fragment.HomeFragment
import com.nuoyun.tnvmue.activity.fragment.MineFragment
import com.nuoyun.tnvmue.activity.fragment.RecommendFragment
import com.nuoyun.tnvmue.adapter.MainAdapter
import kotlinx.android.synthetic.main.activity_main.*
import java.util.*

/**
 * @author JiangAdmin
 * date: 2020/4/10
 * Email: jiangmr@vip.qq.com
 * TODO:
 */
class MainActivity : BaseActivity(), BottomNavigationView.OnNavigationItemSelectedListener, OnPageChangeListener {

    var homeFragment: HomeFragment? = null
    var recommendFragment: RecommendFragment? = null
    var mineFragment: MineFragment? = null

    var fragments: MutableList<Fragment>? = null
    var main_adapter: MainAdapter? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        setDarkStatusIcon(true)
        init()
        main_viewpager!!.currentItem = 0
    }

    private fun init() {
        homeFragment = HomeFragment()
        recommendFragment = RecommendFragment()
        mineFragment = MineFragment()
        fragments = ArrayList()
        fragments!!.add(homeFragment!!)
        fragments!!.add(recommendFragment!!)
        fragments!!.add(mineFragment!!)
        navigation!!.setOnNavigationItemSelectedListener(this)
        navigation!!.itemIconTintList = null
        val fm = supportFragmentManager
        main_adapter = MainAdapter(fm, fragments)
        main_viewpager!!.adapter = main_adapter
        main_viewpager!!.addOnPageChangeListener(this)
    }

    override fun onNavigationItemSelected(item: MenuItem): Boolean {
        when (item.itemId) {
            R.id.navigation_home -> {
                setDarkStatusIcon(true)
                main_viewpager!!.currentItem = 0
                return true
            }
            R.id.navigation_recommend -> {
                setDarkStatusIcon(true)
                main_viewpager!!.currentItem = 1
                return true
            }
            R.id.navigation_mine -> {
                setDarkStatusIcon(true)
                main_viewpager!!.currentItem = 2
                return true
            }

        }
        return false
    }

    override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {}
    override fun onPageSelected(position: Int) {
        when (position) {
            0 -> navigation!!.selectedItemId = R.id.navigation_home
            1 -> navigation!!.selectedItemId = R.id.navigation_recommend
            2 -> navigation!!.selectedItemId = R.id.navigation_mine

        }
    }

    override fun onPageScrollStateChanged(state: Int) {}

    companion object {
        private const val TAG = "MainActivity"
    }
}